package com.houfukude.Activity;

import java.io.File;

import com.houfukude.Draw.ImageDraw;
import com.houfukude.xtestmgr.R;

import Utils.File_Utils;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class ImageDrawActivity extends ActionBarActivity {
	private ImageDraw imageDraw = null;
	private String src;
	private String DEBUG_TAG = "ImageDrawActivity";
	private Intent data;
	private Uri uri;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		Intent intent = getIntent();
		src = intent.getStringExtra("path");

		DisplayMetrics dm = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(dm);

		// Note_Utils.Image_Directory+"20140415_022539.jpg";
		// Log.i(DEBUG_TAG,
		// "screenWidth:"+dm.widthPixels+" screenHeight:"+dm.heightPixels);
		imageDraw = new ImageDraw(this, dm.widthPixels, dm.heightPixels, src);

		// setContentView(R.layout.activity_image_draw);
		// HorizontalScrollView imagedraw_h = (HorizontalScrollView)
		// findViewById(R.id.imagedraw_h);
		// imagedraw_h.removeAllViews();
		// imagedraw_h.addView(imageDraw);
		setContentView(imageDraw);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.draw, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		Log.d(DEBUG_TAG, "ItemSelected item.getItemId():" + item.getItemId());
		switch (item.getItemId()) {
		case R.id.draw_save:
			onSet(1);
			break;
		case R.id.draw_undo:
			onSet(2);
			break;
		case R.id.action_settings:

			Toast.makeText(this, "settings for " + getTitle(),
					Toast.LENGTH_SHORT).show();

			break;
		}
		return super.onOptionsItemSelected(item);
	}

	public void onSet(int flag) {

		switch (flag) {
		case 1:
			Toast.makeText(this, "正在保存...", Toast.LENGTH_LONG).show();
			src = imageDraw.save(getApplicationContext());
			Log.i(DEBUG_TAG, "getMimeType:" + File_Utils.getMimeType(src));
			data = new Intent();
			uri = Uri.fromFile(new File(src));
			data.setData(uri);
			setResult(RESULT_OK, data);
			finish();
			break;
		case 2:
			if (!imageDraw.undo()) {
				Toast.makeText(this, "没有画笔能够撤销", Toast.LENGTH_SHORT).show();
			}
			break;
		case 3:
			data = new Intent();
			uri = Uri.fromFile(new File(src));
			data.setData(uri);
			setResult(RESULT_OK, data);
			finish();
			break;
		default:
			break;
		}

	}

	@Override
	public void onBackPressed() {
		onSet(3);
		// super.onBackPressed();
	}

}
